home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 326-350 / disk_349 / med / source / med200src.zoo / med-tied.c < prev    next >
C/C++ Source or Header  |  1990-04-08  |  12KB  |  377 lines

  1. /* MED - music editor by Teijo Kinnunen 1989, 1990*/
  2. /* med-tied.c: some file handling stuff */
  3. #include "med.h"
  4. extern void __asm Korosta(register __a0 struct Gadget *,register __d0 BOOL);
  5. extern void __asm PaivitaNaytto(register __d0 BOOL);
  6. extern void __asm SetTempo(register __d0 UWORD);
  7. extern struct Kappale far tamakappale;
  8. typedef LONG Fixed;
  9. typedef LONG IFFP;
  10. typedef IFFP ClientProc();
  11. typedef struct _ClientFrame {
  12.     ClientProc *getList,*getProp,*getForm,*getCat; } ClientFrame;
  13. typedef struct { ULONG oneShotHiSamples, repeatHiSamples, samplesPerHiCycle;
  14.     UWORD samplesPerSec; UBYTE ctOctave, sCompression; Fixed volume; }
  15.     Voice8Header;
  16. typedef struct {
  17.    ClientFrame clientFrame;
  18.    UBYTE foundVHDR;
  19.    UBYTE pad1;
  20.    Voice8Header sampHdr;
  21.    } SVXFrame;
  22. extern SVXFrame smusFrame;
  23. extern BYTE *buf;
  24. extern int szBuf;
  25. extern void Ilmoita(),AsetaTempo(),PaivitaSoittimennimi(),NollaaVarit();
  26. extern void SeekSong();
  27. extern UWORD soittimennum,lohkoja;
  28. extern UWORD volatile tempo;
  29. extern ULONG soittimenpituus[];
  30. extern struct Screen *screen;
  31. extern struct Window *window;
  32. extern struct ViewPort *vp;
  33. extern struct Lohko far *lohko[];
  34. extern struct SoundTrackerKappale far ladattu;
  35. extern struct Gadget far gadget3[],far gadget7[];
  36. extern struct Soitin *soitin[];
  37. static UBYTE *soitinlista = NULL;
  38. static ULONG slistanpituus = NULL;
  39. extern BOOL Continue(),AskName();
  40. BOOL LoadInstrument(),LataaKappale(),LataaIFFSoitin(),diskerr();
  41. void LataaSoitinLista(),VapautaSoitinLista(),EtsiHakemisto();
  42. static BOOL kappaleenlatausmenossa = FALSE,LataaLohkot();
  43. static BPTR fh = NULL;
  44. void ConvertTo200();
  45. UWORD uusitoisto,uusitoistonpit;
  46.  
  47. BOOL NewInstrument(ULONG size, UWORD num)
  48. {
  49.     if(soitin[num]) {
  50.         FreeMem((void *)soitin[num],soitin[num]->length +
  51.         sizeof(struct Soitin));
  52.         soitin[num] = NULL;
  53.     }
  54.     soitin[num] = AllocMem(size + sizeof(struct Soitin),
  55.                 MEMF_PUBLIC|MEMF_CHIP|MEMF_CLEAR);
  56.     if(!soitin[num]) return(TRUE);
  57.     soitin[num]->length = size;
  58.     return(FALSE);
  59. }
  60.  
  61. BOOL LoadInstrument(char *name,BOOL src)
  62. {
  63.     ULONG length;
  64.     char latausnimi[100],tunnus[5];
  65.     if(*name == '\0') return(AskName());
  66.     if(src) EtsiHakemisto(tamakappale.soitin[soittimennum],latausnimi);
  67.     else {
  68.         strcpy(latausnimi,name);
  69.         strcpy(tamakappale.soitin[soittimennum],name);
  70.     }
  71.     if(!kappaleenlatausmenossa) {
  72.         tamakappale.stoisto[soittimennum] = uusitoisto;
  73.         tamakappale.stoistonpit[soittimennum] = uusitoistonpit;
  74.     }
  75.     if(!(fh = Open2(latausnimi,MODE_OLDFILE))) return(diskerr(0,0));
  76.     if(Read(fh,tunnus,4) != 4) return(diskerr(fh,"Sample too short."));
  77.     tunnus[4] = '\0';
  78.     if(!strcmp(tunnus,"FORM")) {
  79.         BOOL res = LataaIFFSoitin(fh);
  80.         if(!src) PaivitaSoittimennimi();
  81.         return(res);
  82.     }
  83.     Ilmoita("Loading sample...");
  84.     Seek(fh,0,OFFSET_END);
  85.     length = Seek(fh,0,OFFSET_BEGINNING);
  86.     if(NewInstrument(length,soittimennum)) return(diskerr(0,"No memory."));
  87.     if(Read(fh,(char *)(soitin[soittimennum]) + sizeof(struct Soitin),
  88.         length) != length) {
  89.         FreeMem((void *)soitin[soittimennum],
  90.                 soitin[soittimennum]->length + sizeof(struct Soitin));
  91.         soitin[soittimennum] = NULL;
  92.         return(diskerr(0,0));
  93.     }
  94.     Close(fh); fh = NULL;
  95.     if(!src) PaivitaSoittimennimi();
  96.     return(FALSE);
  97. }
  98.  
  99. BOOL LoadSong(char *nimi)
  100. {
  101.     UWORD scnt;
  102.     char tunnus[5];
  103.     if(*nimi == '\0') return(AskName());
  104.     if(!(fh = Open2(nimi,MODE_OLDFILE))) return(diskerr(0,0));
  105.     if(Read(fh,tunnus,4) != 4) return(diskerr(fh,"This is not a song!"));
  106.     tunnus[4] = '\0';
  107.     Seek(fh,0,OFFSET_BEGINNING);
  108.     if(AlustaKappale(TRUE)) return(diskerr(fh,"Out of memory..."));
  109.     NollaaVarit();
  110.     lohkoja = 0;
  111.     if(!strcmp(tunnus,"MED\x02")) {
  112.         Ilmoita("Loading MED1.12-song...");
  113.         if(Read(fh,(char *)&tamakappale,sizeof(struct Kappale111)) <
  114.             sizeof(struct Kappale111)) return(diskerr(fh,0));
  115.         if(LataaLohkot(FALSE)) return(diskerr(fh,0));
  116.         LoadRGB4(vp,tamakappale.rgb,4);
  117.     } else if(!strcmp(tunnus,"MED\x03")) {
  118.         if(Load200Song(fh)) return(diskerr(fh,0));
  119.         LoadRGB4(vp,tamakappale.rgb,8);
  120.     } else {
  121.         if(!strcmp(tunnus,"SONG")) Ilmoita("Loading SoundFX-song...");
  122.         else Ilmoita("Loading SoundTracker-song...");
  123.         if(Read(fh,(char *)&ladattu,sizeof(struct SoundTrackerKappale)) <
  124.             sizeof(struct SoundTrackerKappale)) return(diskerr(fh,"This is not a song!"));
  125.         if(LataaLohkot(TRUE)) return(diskerr(fh,0));
  126.         tamakappale.lohkoja = lohkoja;
  127.         tamakappale.tempo = 6;
  128.         tamakappale.kappaleen_pituus = ladattu.st_kappaleen_pit;
  129.         tamakappale.vaihtoja = 5;
  130.         memcpy((char *)(tamakappale.soittojarjestys),
  131.             (char *)(ladattu.st_lohkojarj),100);
  132.         for(scnt = 0; scnt < 15; scnt++) {
  133.             tamakappale.soitin[scnt+1][0] = '\0';
  134.             tamakappale.soittimenvoimakkuus[scnt+1] = ladattu.st_ins[scnt].sti_vol;
  135.             tamakappale.stoisto[scnt+1] = ladattu.st_ins[scnt].sti_repeat;
  136.             tamakappale.stoistonpit[scnt+1] = ladattu.st_ins[scnt].sti_replen * 2;
  137.             strcpy(tamakappale.soitin[scnt+1],ladattu.st_ins[scnt].sti_nimi);
  138.         }
  139.     }
  140.     AsetaSuodatin();
  141.     Korosta(&gadget3[7],(BOOL)(tamakappale.vaihtoja == 6 ? FALSE : TRUE));
  142.     Korosta(&gadget3[8],(BOOL)(tamakappale.vaihtoja == 6 ? TRUE : FALSE));
  143.     UpdateJumpGadgets();
  144.     PaivitaNaytto(TRUE);
  145.     soittimennum = 1;
  146.     SetTempo(tamakappale.tempo);
  147.     PaivitaSoittimennimi();
  148.     kappaleenlatausmenossa = TRUE; /*vasta nyt voi virherequesterissa jatkaa*/
  149.     if(tamakappale.liput & LIPPU_SOITTIMETMUKANA) LoadAttachedInstrs(fh);
  150.     Close(fh); fh = NULL;
  151.     if(!(tamakappale.liput & LIPPU_SOITTIMETMUKANA)) {
  152.         for(scnt = 0; scnt < 31; scnt++) {
  153.             if(tamakappale.soitin[scnt+1][0] && !tamakappale.midikanava[scnt+1])
  154.                 if(LoadInstrument(tamakappale.
  155.                     soitin[scnt+1],TRUE) && !Continue()) break;
  156.             SeurSoitin();
  157.         }
  158.     }
  159.     soittimennum = 1;
  160.     PaivitaSoittimennimi();
  161.     TulostaLohkoJaSoitin();
  162.     Ilmoita("Song is now loaded.");
  163.     kappaleenlatausmenossa = FALSE;
  164.     return(FALSE);
  165. }
  166.  
  167. BOOL LataaLohkot(st)
  168. BOOL st; /* SoundTracker ?? */
  169. {
  170.     long jaljella = Seek(fh,0,OFFSET_END),lpt1 = 1024,lpt2 = 1028;
  171.     BOOL error = FALSE;
  172.     ULONG *convblk = AllocMem(1024 + 4,MEMF_PUBLIC|MEMF_CLEAR);
  173.     if(!convblk) return(TRUE);    /* no memory */
  174.     jaljella = Seek(fh,jaljella,OFFSET_BEGINNING) - jaljella;
  175.     for(;;) {
  176.         if(lohkoja >= 100) return(TRUE); /* Apua...ei enää!!!! */
  177.         memset((void *)convblk,0,1028);
  178.         if(st) {
  179.             *convblk = 0L;
  180.             if(lpt1 > jaljella) lpt1 = jaljella;
  181.             if(Read(fh,(char *)(convblk + 1),lpt1)
  182.                 < lpt1) { error = TRUE; break; }
  183.             if((jaljella -= 1024) <= 0) break; /* 0 jäljellä */
  184.         } else {
  185.             if(lpt2 > jaljella) lpt2 = jaljella;
  186.             if(Read(fh,(char *)convblk,lpt2) < lpt2)
  187.                 { error = TRUE; break; }
  188.             if((jaljella -= 1028) <= 0) break;
  189.         }
  190.         ConvertTo200((UBYTE *)convblk,(UBYTE *)lohko[lohkoja]);
  191.         if(AllocBlock(++lohkoja,4)) {
  192.             error = TRUE; /* Out of memory!!*/
  193.             break;
  194.         }
  195.     }
  196.     ConvertTo200((UBYTE *)convblk,(UBYTE *)lohko[lohkoja]);
  197.     lohkoja++;
  198.     FreeMem((void *)convblk,1028);
  199.     return(error);
  200. }
  201.  
  202. BOOL diskerr(BPTR fh,char *txt)
  203. {
  204.     char virheteksti[45],*virheptr;
  205.     DisplayBeep(screen);
  206.     strcpy(virheteksti,"Error: ");
  207.     switch(IoErr()) {
  208.         case 103:    virheptr = "Insufficient free store."; break;
  209.         case 202:    virheptr = "Object in use."; break;
  210.         case 203:    virheptr = "Object already exists."; break;
  211.         case 204:    virheptr = "Directory not found."; break;
  212.         case 205:    virheptr = "Object not found."; break;
  213.         case 210:    virheptr = "Invalid name."; break;
  214.         case 212:    virheptr = "Not of required type."; break;
  215.         case 213:    virheptr = "Disk not validated."; break;
  216.         case 214:    virheptr = "Disk write-protected."; break;
  217.         case 218:    virheptr = "Device not mounted."; break;
  218.         case 221:    virheptr = "Disk full."; break;
  219.         case 222:    virheptr = "File is delete-protected."; break;
  220.         case 223:    virheptr = "File is write-protected."; break;
  221.         case 224:    virheptr = "File is read-protected."; break;
  222.         case 225:    virheptr = "Not a DOS disk."; break;
  223.         case 226:    virheptr = "No disk in drive."; break;
  224.         default:    if(txt) virheptr = txt;
  225.                 else virheptr = "Something is wrong.";
  226.     }            /* A very informative error msg -^^ */
  227.     if(fh) Close(fh);
  228.     strcpy(virheteksti+7,virheptr);
  229.     Ilmoita(virheteksti);
  230.     return(TRUE);
  231. }
  232.  
  233. BOOL LataaIFFSoitin()
  234. {
  235.     Seek(fh,0,OFFSET_BEGINNING);
  236.     Ilmoita("Loading IFF-instrument...");
  237.     if(soitin[soittimennum]) {
  238.         FreeMem((void *)soitin[soittimennum],
  239.             soitin[soittimennum]->length + sizeof(struct Soitin));
  240.         soitin[soittimennum] = NULL;
  241.     }
  242.     buf = NULL; szBuf = NULL;
  243.     (void)ReadSample(fh);
  244.     Close(fh); fh = NULL;
  245.     if(!buf)    return(diskerr(0,"Failed to load!!!"));
  246.     soitin[soittimennum] = (struct Soitin *)buf;
  247.     soitin[soittimennum]->length = szBuf;
  248.     switch(smusFrame.sampHdr.ctOctave) {
  249.         case 5:    soitin[soittimennum]->type = IFF5OCT;    break;
  250.         case 3:    soitin[soittimennum]->type = IFF3OCT;    break;
  251.         default: soitin[soittimennum]->type = 0;
  252.     }
  253.     if(kappaleenlatausmenossa); /* <- tiedot ovat jo kappaleessa */
  254.     else if(smusFrame.sampHdr.repeatHiSamples) {
  255.         tamakappale.stoisto[soittimennum] = smusFrame.sampHdr.oneShotHiSamples;
  256.         tamakappale.stoistonpit[soittimennum] = smusFrame.sampHdr.repeatHiSamples;
  257.         tamakappale.soittimenvoimakkuus[soittimennum] = 64; /* Täysillä vaan */
  258.     } else {
  259.         tamakappale.stoisto[soittimennum] = 0;
  260.         tamakappale.stoistonpit[soittimennum] = 0;
  261.         tamakappale.soittimenvoimakkuus[soittimennum] = 64;
  262.     }
  263.     return(FALSE);
  264. }
  265.  
  266. void LataaSoitinLista()
  267. {
  268.     UBYTE *scanptr;
  269.     fh = Open("MED_paths",MODE_OLDFILE);
  270.     if(!fh) fh = Open("S:MED_paths",MODE_OLDFILE);
  271.     if(!fh) {
  272.         Ilmoita("MED_paths not found.");
  273.         return; /* ei löytynyt */
  274.     }
  275.     Seek(fh,0,OFFSET_END);
  276.     slistanpituus = Seek(fh,0,OFFSET_BEGINNING);
  277.     if(!(soitinlista = AllocMem(slistanpituus,MEMF_PUBLIC))) {
  278.         Close(fh);
  279.         fh = NULL;
  280.         return;
  281.     }
  282.     Read(fh,soitinlista,slistanpituus);
  283.     Close(fh);
  284.     fh = NULL;
  285.     for(scanptr=soitinlista;scanptr<soitinlista+slistanpituus;scanptr++) {
  286.         if(*scanptr == '\n') *scanptr = '\0';
  287.         if(*scanptr == ':' && *(scanptr+1) == ':') *scanptr = '\0';
  288.         if(*scanptr >= 'a' && *scanptr <= 'z')
  289.             *scanptr = *scanptr - 'a' + 'A'; /* isoiksi */
  290.         /* rivinvaihdot 0:ksi */
  291.     }
  292. }
  293.  
  294. void VapautaSoitinLista()
  295. {
  296.     if(soitinlista) FreeMem((void *)soitinlista,slistanpituus);
  297. }
  298.  
  299. void EtsiHakemisto(nimi,mihin)
  300. UBYTE *nimi,*mihin;
  301. {
  302.     UBYTE *scanptr = soitinlista,*hakemisto = NULL,isonimi[40],*isoksi = &isonimi[0];
  303.     UBYTE mikanumero = 0;
  304.     BOOL loytyi = FALSE;
  305.     uusitoisto = uusitoistonpit = 0;
  306.     if(!scanptr) { /* soitinlistaa ei ole ladattu */
  307.         strcpy(mihin,nimi);
  308.         return;
  309.     }
  310.     if(*nimi == '*' || *nimi == ':') {
  311.         *mihin = '\0'; /* ei hyväksytä! */
  312.         return;
  313.     }
  314.     strcpy(isonimi,nimi);
  315.     while(*isoksi) {
  316.         if(*isoksi >= 'a' && *isoksi <= 'z')
  317.             *isoksi = *isoksi - 'a' + 'A'; /*muutetaan isoksi*/
  318.         isoksi++;
  319.     }
  320.     while(scanptr < soitinlista + slistanpituus) {
  321.         if(*scanptr == '*') hakemisto = scanptr;
  322.         else if(*scanptr == isonimi[0]) { /* tutkitaan 1.merkki */
  323.             if(!strcmp(scanptr,isonimi)) {
  324.                 loytyi = TRUE; /* löytyi !! */
  325.                 break;
  326.             }
  327.         }
  328.         while(*scanptr++ != '\0'); /* etsitään seuraava */
  329.     }
  330.     if(!loytyi) {
  331.         strcpy(mihin,nimi); /* ei löytynyt, kokeillaan sellaisenaan */
  332.         return;
  333.     }
  334.     /* löytyi!! */
  335.     if(hakemisto) strcpy(mihin,hakemisto+1);
  336.         /* kopioidaan hakemiston nimi (alussa '*') */
  337.     strcat(mihin,nimi); /* ja lisätään nimi */
  338.     if(*(scanptr + strlen(scanptr) + 1) == ':') { /* toisto! */
  339.         scanptr += strlen(scanptr) + 1;
  340.         while(*scanptr) {
  341.             if(*scanptr == '/') mikanumero = 1; /* pituus.. */
  342.             if(*scanptr >= '0' && *scanptr <= '9')
  343.                 switch(mikanumero) {
  344.                     case 0:
  345.                     uusitoisto *= 10;
  346.                     uusitoisto += ((*scanptr) - '0');
  347.                     break;
  348.                     case 1:
  349.                     uusitoistonpit *= 10;
  350.                     uusitoistonpit += ((*scanptr)-'0');
  351.                 }
  352.             scanptr++;
  353.         }
  354.     }
  355. }
  356.  
  357. void ConvertTo200(from,to)
  358. UBYTE *from,*to;
  359. {
  360.     extern UWORD periodit[];
  361.     register UWORD testper,count;
  362.     register UBYTE newnotenum = 0,instrG_V = 0;
  363.     from += 4;    /* skip 4 bytes */
  364.     to += 4;
  365.     for(count = 0; count < (64 * 4); count++) {
  366.         testper = *((UWORD *)from);
  367.         from += 2;
  368.         newnotenum = instrG_V = 0;
  369.         if(testper & 0x9000) { testper &= ~0x9000; instrG_V = 1; }
  370.         if(testper)    while(testper < periodit[newnotenum++]);
  371.         if(instrG_V) newnotenum |= 0x80;
  372.         *to++ = newnotenum;
  373.         *to++ = *from++;
  374.         *to++ = *from++;
  375.     }
  376. }
  377.